home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 002 / alib / alib.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  11KB  |  395 lines

  1. /*
  2.  * ALIB Rev. 1.0
  3.  * Amiga Object Module Librarian
  4.  * Programmed by Mike Schwartz
  5.  *               (C)1985, MS Software, all rights reserved.
  6.  *
  7.  * Feel free to distribute this program on a no-charge basis.
  8.  *
  9.  * Use:
  10.  *    ALIB  option  library filelist
  11.  * Where
  12.  *    option may be one of the following:
  13.  *       D  =  delete object modules from library
  14.  *       R  =  replace object modules (or add if not defined)
  15.  *       L  =  directory of library
  16.  *    library is the name of the library file to be created or modified.
  17.  *       if no .lib extension is provided on the command line, it is
  18.  *       appended.  Also note that a file library.dir is created for
  19.  *       each library file and is managed by this program.
  20.  *    filelist is a list of object module filenames separated by spaces
  21.  *       or commas.  Note that if .o is not present in the filenames,
  22.  *       .o will be appended.
  23.  */
  24. #include "stdio.h"
  25. #include "fcntl.h"
  26.  
  27. /*
  28.  * The following structure defines the records in the .dir file for
  29.  * the library.
  30.  */
  31. #define  LD    struct   libdir_def
  32. struct   libdir_def     {
  33.    char  object_filename[80];       /* name of .o file */
  34.    long  module_offset;             /* offset of module in .lib file */
  35.    long  module_size;               /* size of module in bytes */
  36.    };
  37.  
  38. /*
  39.  * When the library is openned, each object module is read into memory
  40.  * and managed with the following structure.
  41.  */
  42. #define  LL    struct   liblist_def
  43. struct   liblist_def    {
  44.    struct   libdir_def  dir_entry;  /* directory entry for module */
  45.    char     *object_module;         /* actual text of module */
  46.    LL       *next;                  /* pointer to next in list */
  47.    };
  48.  
  49. /*
  50.  * external functions
  51.  */
  52. extern   char  *getmem();
  53. extern   long  lseek();
  54.  
  55. /*
  56.  * Global variables
  57.  */
  58. LL    *objlist=NULL;                /* linked list of object modules */
  59.  
  60. char  libname[80];                  /* name of library file */
  61. int   libfd;                        /* file descriptor for library file */
  62. char  objname[80];                  /* name of object file */
  63. int   objfd;                        /* file descriptor for object file */
  64. char  dirname[80];                  /* name of directory file */
  65. int   dirfd;                        /* file descriptor for directory file */
  66.  
  67. char  buf[512];                     /* misc buffer */
  68.  
  69. char  option;                       /* user selected option */
  70. /*
  71.  * This function prints the help messages and exits.
  72.  */
  73. help() {
  74.    printf("Use:\n");
  75.    printf("   ALIB  option  library filelist \n");
  76.    printf("Where\n");
  77.    printf("   option may be one of the following:\n");
  78.    printf("      D  =  delete object modules from library\n");
  79.    printf("      R  =  replace object modules (or add if not defined) \n");
  80.    printf("      L  =  directory of library\n");
  81.    printf("   library is the name of the library file to be created or modified.\n");
  82.    printf("      if no .lib extension is provided on the command line, it is \n");
  83.    printf("      appended.  Also note that a file library.dir is created for\n");
  84.    printf("      each library file and is managed by this program. \n");
  85.    printf("   filelist is a list of object module filenames separated by spaces\n");
  86.    printf("      or commas.  Note that if .o is not present in the filenames, \n");
  87.    printf("      .o will be appended. \n");
  88.    exit(1);
  89.    }
  90.  
  91. /*
  92.  * The following function opens the library and directory file.  If the
  93.  * file does not exist, then the user is asked if he wants to create it.
  94.  * If the library exists, then it is read in and built in a linked list.
  95.  */
  96. open_library() {
  97.    LL    *lp;           /* pointer to module node */
  98.  
  99.    /*
  100.     * Open the library file and directory file.
  101.     */
  102.    libfd = open(libname, O_RDONLY | O_RAW);
  103.    if (libfd == -1) {
  104.       /*
  105.        * Library does not exist, prompt user for creation.
  106.        */
  107.       while (1) {
  108.          printf("%s does not exist, create it? (Y/N): ", libname);
  109.          gets(buf);
  110.          if (buf[0] == 'y' || buf[0] == 'Y')
  111.             return !0;
  112.          if (buf[0] == 'n' || buf[0] == 'N') {
  113.             printf("Alib abandoned\n");
  114.             exit(1);
  115.             }
  116.          }
  117.       }
  118.    dirfd = open(dirname, O_RDONLY | O_RAW);
  119.    if (dirfd == -1) {
  120.       printf("%s has been corrupted\n", dirname);
  121.       printf("Alib abandoned\n");
  122.       exit(1);
  123.       }
  124.    /*
  125.     * library and directory files are open, read in the library.
  126.     */
  127.    while (1) {
  128.       /*
  129.        * Allocate a node to hold the object module.
  130.        */
  131.       lp = (LL *)getmem(sizeof(LL));
  132.       if (lp == NULL) {
  133.          printf("Not enough memory.\nAlib abandoned\n");
  134.          exit(1);
  135.          }
  136.       /*
  137.        * Check for end of file.
  138.        */
  139.       if (read(dirfd, (char *)&lp->dir_entry, sizeof(LD)) != sizeof(LD))
  140.          break;
  141.       /*
  142.        * Allocate a buffer to hold the object file image.
  143.        */
  144.       lp->object_module = getmem(lp->dir_entry.module_size);
  145.       if (lp->object_module == NULL) {
  146.          printf("Not enough memory.\nAlib abandoned\n");
  147.          exit(1);
  148.          }
  149.       /*
  150.        * Read in the object module
  151.        */
  152.       if (read(libfd, lp->object_module, lp->dir_entry.module_size) != 
  153.                  lp->dir_entry.module_size) {
  154.          printf("Library file is corrupted.\nAlib abandoned\n");
  155.          exit(1);
  156.          }
  157.       /*
  158.        * Add module to linked list.
  159.        */
  160.       lp->next = objlist;
  161.       objlist = lp;
  162.       }
  163.    /*
  164.     * Library has been read in ok.  Close files and return.
  165.     */
  166.    close(libfd);
  167.    close(dirfd);
  168.    printf("Library file read in ok\n");
  169.    }
  170.  
  171. /*
  172.  * The following function removes an object module from the linked list
  173.  * and frees up any memory used by it.
  174.  */
  175. kill_module(name)
  176. char  *name;
  177. {
  178.    LL    *lp;
  179.    LL    *lp2;
  180.  
  181.    if (objlist == NULL)
  182.       return 0;
  183.    strcpy(objname, name);
  184.    strcat(objname, ".o");
  185.    if (strcmp(objlist->dir_entry.object_filename,  objname) == 0) {
  186.       lp = objlist->next;
  187.       rlsmem(objlist->object_module, objlist->dir_entry.module_size);
  188.       rlsmem((char *)objlist, sizeof(LL));
  189.       objlist = lp;
  190.       return !0;
  191.       }
  192.    for (lp = objlist; lp->next != NULL; lp=lp->next)
  193.       if (strcmp(lp->next->dir_entry.object_filename, objname) == 0) {
  194.          lp2 = lp->next;
  195.          lp->next = lp2->next;
  196.          rlsmem(lp2->object_module, lp2->dir_entry.module_size);
  197.          rlsmem((char *)lp2, sizeof(LL));
  198.          return !0;
  199.          }
  200.    return 0;
  201.    }
  202.  
  203. /*
  204.  * The following function
  205.  */
  206. add_module(name)
  207. char  *name;
  208. {
  209.    LL    *lp;
  210.    int   len;
  211.  
  212.    strcpy(objname, name);
  213.    strcat(objname, ".o");
  214. printf("adding %s\n", objname);
  215.  
  216.    /*
  217.     * Open the object file
  218.     */
  219.    objfd = open(objname, O_RDONLY | O_RAW);
  220.    if (objfd == -1) {
  221.       printf("%s cannot be added\nAlib abandoned\n", objname);
  222.       exit(1);
  223.       }
  224.    /*
  225.     * Allocate a node to hold the file.
  226.     */
  227.    lp = (LL *)getmem(sizeof(LL));
  228.    if (lp == NULL) {
  229.       printf("Not enough memory.\nAlib abandoned\n");
  230.       exit(1);
  231.       }
  232.    /*
  233.     * Initialize it.
  234.     */
  235.    strcpy(lp->dir_entry.object_filename, objname);
  236.    lp->dir_entry.module_size = lseek(objfd, 0, 2);
  237. printf("module size = %d\n", lp->dir_entry.module_size);
  238.    lseek(objfd, 0, 0);
  239.    /*
  240.     * Allocate the buffer to read the object module into.
  241.     */
  242.    lp->object_module = getmem(lp->dir_entry.module_size);
  243.    if (lp->object_module == NULL) {
  244.       printf("Not enough memory.\nAlib abandoned\n");
  245.       exit(1);
  246.       }
  247.    /*
  248.     * Read in the file.
  249.     */
  250.    if (read(objfd, lp->object_module, lp->dir_entry.module_size) != 
  251.              lp->dir_entry.module_size) {
  252.       printf("Library file is corrupted.\nAlib abandoned\n");
  253.       exit(1);
  254.       }
  255.    /*
  256.     * Add module to linked list.
  257.     */
  258.    lp->next = objlist;
  259.    objlist = lp;
  260.    /*
  261.     * Close object file and return.
  262.     */
  263.    close(objfd);
  264.    printf("%s added\n", name);
  265.    }
  266.  
  267. /*
  268.  * The following routine writes the library from memory and exits.
  269.  */
  270. close_library() {
  271.    LL    *lp;
  272.    long  file_position;
  273.    long  length;
  274.  
  275.    /*
  276.     * Open the library
  277.     */
  278.    libfd = open(libname, O_CREAT | O_RAW | O_WRONLY);
  279.    if (libfd == -1) {
  280.       printf("Error openning %s for output\nAlib abandoned", libname);
  281.       exit(1);
  282.       }
  283.    dirfd = open(dirname, O_CREAT | O_RAW | O_WRONLY);
  284.    if (dirfd == -1) {
  285.       printf("Error openning %s for output\nAlib abandoned", dirname);
  286.       exit(1);
  287.       }
  288.    /*
  289.     * Write out the individual modules and directory records.
  290.     */
  291.    file_position = 0;
  292.    for (lp = objlist; lp != NULL; lp = lp->next) {
  293.       length = write(libfd, lp->object_module, lp->dir_entry.module_size);
  294.       if (length != lp->dir_entry.module_size) {
  295.          printf("Error writing to library file\nAlib abandoned\n");
  296.          exit(1);
  297.          }
  298.       lp->dir_entry.module_offset = file_position;
  299.       file_position += length;
  300.       if (write(dirfd, (char *)&lp->dir_entry, sizeof(LD)) != sizeof(LD)) {
  301.          printf("Error writing to directory file\nAlib abandoned\n");
  302.          exit(1);
  303.          }
  304.       }
  305.    /*
  306.     * Close the library files
  307.     */
  308.    close(libfd);
  309.    close(dirfd);
  310.    printf("library is %ld bytes\nAlib complete\n", file_position);
  311.    exit(0);
  312.    }
  313.  
  314.  
  315. /*
  316.  * Main program.
  317.  */
  318. main(argc, argv)
  319. int   argc;
  320. char  *argv[];
  321. {
  322.    int   count;
  323.  
  324.    printf("ALIB Rev. 1.0 \n");
  325.    printf("Amiga Object Module Librarian\n");
  326.    printf("Programmed by Mike Schwartz\n");
  327.    printf("(C)1985 MS Software, all rights reserved\n");
  328.  
  329.    /*
  330.     * check for command line parameters present.
  331.     */
  332.    if (argc < 3)
  333.       help();
  334.    /*
  335.     * setup the option and filenames
  336.     */
  337.    option = argv[1][0];
  338.    strcpy(libname, argv[2]);
  339.    strcpy(dirname, argv[2]);
  340.    strcat(libname, ".lib");
  341.    strcat(dirname, ".dir");
  342.    if (option == 'l' || option == 'L') {
  343.       directory();
  344.       exit(0);
  345.       }
  346.    if (option != 'd' && option != 'D' && option != 'r' && option != 'R')
  347.       help();
  348.    /*
  349.     * Open the library file
  350.     */
  351.    open_library();
  352.    for (count = 3; count < argc; count++) {
  353.       switch(option) {
  354.          case 'd':
  355.          case 'D':
  356.             printf("deleting %s from library\n", argv[count]);
  357.             if (!kill_module(argv[count]))
  358.                printf("%s not defined in library\n", argv[count]);
  359.             else
  360.                printf("deleted.\n");
  361.             break;
  362.          case 'r':
  363.          case 'R':
  364.             kill_module(argv[count]);
  365.             add_module(argv[count]);
  366.             break;
  367.          }
  368.       }
  369.    close_library();
  370.    }
  371.  
  372. directory() {
  373.    LD    d_entry;
  374.    int   count;
  375.    int   bytes;
  376.  
  377.    dirfd = open(dirname, O_RDONLY | O_RAW);
  378.    if (dirfd == -1) {
  379.       printf("%s not found\n", dirname);
  380.       exit(1);
  381.       }
  382.    printf("Directory of library %s\n", libname);
  383.    printf("Module Name                    Size   Offset\n");
  384.    count = bytes = 0;
  385.    while (read(dirfd, (char *)&d_entry, sizeof(LD)) == sizeof(LD)) {
  386.       printf("%-30.30s %-6d %-6d\n", d_entry.object_filename, 
  387.                d_entry.module_size, d_entry.module_offset);
  388.       count++;
  389.       bytes += d_entry.module_size;
  390.       }
  391.    printf("Library consists of %d entries totalling %d bytes\n", count,
  392.             bytes);
  393.    close(dirfd);
  394.    }
  395.